package com.atguigu.crowd.handler;

import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.api.RedisRemoteService;
import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.po.MemberPO;
import com.atguigu.crowd.entity.vo.PortalTypeVO;
import com.atguigu.crowd.entity.vo.RankVO;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

//首页
@Controller
public class PortalHandler {
    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    @Autowired
   private RedisRemoteService redisRemoteService;


    @Autowired
    private RankVO rankVO;


    //排行榜 刷新与显示 15分钟一刷新
    @Scheduled(fixedRate = 1000*60*15)
    public void showRankWithTime(){ Date currentTime = new Date();

        //先获取排行榜前5 项目名称
        ResultEntity<String> hotIDRange = redisRemoteService.getRedisRangeRemote("hotID", 0, 4);


        //重置排行
        redisRemoteService.setRedisZsetAllRemote("hotID",0);


        //2.检查查询结果
        if(ResultEntity.SUCCESS.equals(hotIDRange.getResult())){
            //3.拿到查询到的结果   项目名称
            String rangeData = hotIDRange.getData();

            String[] split = rangeData.split(",");

            rankVO.setRank1Name(split[0].substring(1));
            rankVO.setRank2Name(split[1]);
            rankVO.setRank3Name(split[2]);
            rankVO.setRank4Name(split[3]);
            rankVO.setRank5Name(split[4].substring(0,split[4].length()-1));

//            modelMap.addAttribute(CrowdConstant.ATTR_NAME_RANK_DATA,rankVO);
        }



    }




    @RequestMapping("/") //使通过域名可以直接访问到 首页
    public String showPortalPage(ModelMap modelMap,
                                 HttpSession session) {

        //从数据库中获取数据
        //1.调用mysql-provider提供的 查询分类VO的 首页要显示信息的接口
        ResultEntity<List<PortalTypeVO>> resultEntity = mySQLRemoteService.getPortalTypeProjectDataRemote();

        //2.检查查询结果
        if(ResultEntity.SUCCESS.equals(resultEntity.getResult())){
            //3.拿到查询到的结果
            List<PortalTypeVO> list = resultEntity.getData();

            //4.存入request域 以便在首页进行填充
            modelMap.addAttribute(CrowdConstant.ATTR_NAME_PORTAL_DATA,list);

//            //放入session 在更多页面用
//            session.setAttribute(CrowdConstant.ATTR_NAME_PORTAL_DATA,list);


        }


        ResultEntity<String> hotIDRange = redisRemoteService.getRedisRangeRemote("hotID", 0, 4);

        //2.检查查询结果
        if(ResultEntity.SUCCESS.equals(hotIDRange.getResult())){
            //3.拿到查询到的结果   项目名称
            String rangeData = hotIDRange.getData();

            String[] split = rangeData.split(",");


            rankVO.setRank1Name(split[0].substring(1));
            rankVO.setRank2Name(split[1]);
            rankVO.setRank3Name(split[2]);
            rankVO.setRank4Name(split[3]);
            rankVO.setRank5Name(split[4].substring(0,split[4].length()-1));

            //4.存入request域 以便在首页进行填充
            modelMap.addAttribute(CrowdConstant.ATTR_NAME_RANK_DATA,rankVO);

        }

        return "portal";
    }




}
